package com.example.test.exam;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.Admin_function.add_test;
import com.example.test.R;
import com.example.test.all_Myhelper.exam_Myhelper;
import com.example.test.bottom_tab.tab_main_activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class all_tiku extends AppCompatActivity {
    exam_Myhelper exam_myhelper;
    public TextView zhengquexuanxiang,timu_list,timu_id;
    public Button del_timu,back;
    public ListView mListView;
//    public String[] title = {"测试时间：10min","测试时间：1h","测试时间：2h"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exam_myhelper = new exam_Myhelper(this);
        init();
        tiku_MyBaseAdapter myBaseAdapter = new tiku_MyBaseAdapter();
        mListView.setAdapter(myBaseAdapter);
    }
    private void init(){
        zhengquexuanxiang = (TextView) findViewById(R.id.correct_list);
        timu_list = (TextView) findViewById(R.id.timu_list);
        del_timu = (Button) findViewById(R.id.del_timu);
        mListView = (ListView) findViewById(R.id.lv);
        timu_id = (TextView) findViewById(R.id.timu_id);
        back = (Button) findViewById(R.id.exam_main_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent test_add = new Intent(all_tiku.this, tab_main_activity.class);
                startActivity(test_add);
            }
        });
    }

    public List<String> titles(){
        SQLiteDatabase db;
        int i=0;
        db = exam_myhelper.getReadableDatabase();
        Cursor cursor = db.query("tiku",null,null,null,null,null,null);
        String[] all_titles = new String[cursor.getCount()];
//        cursor.moveToFirst();
//        all_titles[0]= cursor.getString(1);
        while (cursor.moveToNext()){
            all_titles[i] = cursor.getString(1);
            i++;
        }
        List<String> list = Arrays.asList(all_titles);
        cursor.close();
        db.close();
        return list;
    }

    public List<String> id_list(){
        SQLiteDatabase db;
        int i=0;
        db = exam_myhelper.getReadableDatabase();
        Cursor cursor = db.query("tiku",null,null,null,null,null,null);
        String[] list_id = new String[cursor.getCount()];
//        cursor.moveToFirst();
//        list_id[0] = cursor.getInt(0);
        while (cursor.moveToNext()){
            list_id[i] = cursor.getString(0);
//            System.out.println("输出是："+list_id[i]);
            i++;
        }
        List<String> list = Arrays.asList(list_id);
        cursor.close();
        db.close();
        return list;
    }

    public List<String> xuanxiang_list(){
        SQLiteDatabase db;
        int i=0;
        db = exam_myhelper.getReadableDatabase();
        Cursor cursor = db.query("tiku",null,null,null,null,null,null);
        String[] list_xuanxiang = new String[cursor.getCount()];
        while (cursor.moveToNext()){
            list_xuanxiang[i] = cursor.getString(6);
            System.out.println("输出是："+list_xuanxiang[i]);
            i++;
        }
        List<String> list = Arrays.asList(list_xuanxiang);
        cursor.close();
        db.close();
        return list;
    }

    public static class ViewHolder{
        public TextView xuanxiang,timu,xuhao;
        public Button bt;

    }

    class tiku_MyBaseAdapter extends BaseAdapter{
        public List<String> title = new ArrayList<>(titles());
        public List<String> id = new ArrayList<>(id_list());
        public List<String> correct = new ArrayList<>(xuanxiang_list());


        @Override
        public int getCount() {
            return title.size();
        }

        @Override
        public Object getItem(int position) {
            return title.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                convertView = View.inflate(all_tiku.this,R.layout.list_tiku,null);
                holder = new ViewHolder();
                holder.timu = (TextView) convertView.findViewById(R.id.timu_list);
                holder.xuhao = (TextView) convertView.findViewById(R.id.timu_id);
                holder.xuanxiang = (TextView) convertView.findViewById(R.id.correct_list);
                holder.bt = (Button) convertView.findViewById(R.id.del_timu);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
                holder.timu.setText("题目："+"\n"+title.get(position));
                holder.xuhao.setText("_id："+id.get(position));
                holder.xuanxiang.setText("正确选项是："+correct.get(position));
                holder.bt.setTag(position);
                holder.bt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String del_timu = id.get(position);
                        System.out.println("结果是："+del_timu);
                        SQLiteDatabase db;
                        db = exam_myhelper.getReadableDatabase();
                        db.delete("tiku","_id=?",new String[]{del_timu});
//                        db.execSQL("update sqlite_sequence set _id=0 where name = 'tiku'");
                        Toast.makeText(all_tiku.this,"题目已删除",Toast.LENGTH_SHORT).show();
                        db.close();
                        Intent intent = new Intent(all_tiku.this,all_tiku.class);
                        startActivity(intent);

                    }
                });
            return convertView;
        }
    }
}